package com.myproject0616chat;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketUtil{ 
   private static Socket socket;
   private static Receriver receriver;
	
//	static{
//		
//		
//		
//		
//		new Thread(new Runnable() {
//			@SuppressWarnings("resource")
//			public void run() {
//				try {
//					
//					ServerSocket serversocket = new ServerSocket(1234);
//				       socket = serversocket.accept();
//				       receriver.startReceriver();
//				       System.out.println("服务端");
//				} catch (IOException e) {					
//					try {
//						socket=new Socket("192.168.1.112", 1234);
//						receriver.startReceriver();
//						System.out.println("客户端");
//					} catch (UnknownHostException e1) {
//						// TODO Auto-generated catch block
//						e1.printStackTrace();
//					} catch (IOException e1) {
//						// TODO Auto-generated catch block
//						e1.printStackTrace();
//					}
//					
//				}
//			
//			}
//		}).start();
//
//	}
	
	public static void linking(){
		try {
			socket=new Socket("192.168.1.112",22222);
			System.out.println("ok");
			receriver.startReceriver();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static Socket getSocket() {
		return socket;
	}

	public static void setSocket(Socket socket) {
		SocketUtil.socket = socket;
	}
	
	
	public static void sendMessage(Object message) {
		
		try {
			ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
			oos.writeObject(message);
			oos.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}

	public static void setReceriver(Receriver receriver) {
		SocketUtil.receriver = receriver;
	}
	
	
}
